package com.example.shop_2304.getPre

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.base.param.PCreateOrder
import com.example.shop_2304.R
import com.example.shop_2304.adapter.GetPreAdapter
import com.example.shop_2304.databinding.ActivityGetPreBinding

@Route(path = "/app/GetPreActivity")
class GetPreActivity : BaseActivity<ActivityGetPreBinding,GetPreViewModel>() {
    //从购物车界面传入一个字段 preOrderNo

    @Autowired
    @JvmField
    var preOrderNo = ""

    lateinit var getPreAdapter: GetPreAdapter

    override fun initView() {
        ARouter.getInstance().inject(this)


        getPreAdapter = GetPreAdapter()
        binding.rv.adapter = getPreAdapter
        binding.rv.layoutManager = LinearLayoutManager(this)
        viewModel.getPreOrder(preOrderNo)

        //点击 提交订单 调用创建订单接口，跳转到支付界面
        binding.createOrder.setOnClickListener {
            val pCreateOrder = PCreateOrder()
            pCreateOrder.preOrderNo = preOrderNo //如果地址改变了，addressId需要重新赋值  如果选择了优惠券，couponId要发生变化
            viewModel.createOrder(pCreateOrder)
        }
    }

    override fun initData() {
        viewModel.getPreSuccess.observe(this){
            if(it.code==200){
                //用户名称显示
                binding.userName.text = "${it.data.orderInfoVo.realName}  ${it.data.orderInfoVo.phone}"
                //用户地址显示
                binding.userAddress.text = "${it.data.orderInfoVo.province}${it.data.orderInfoVo.city}${it.data.orderInfoVo.district}${it.data.orderInfoVo.detail}"
                //共有几件商品
                binding.tvCount.text = "共有${it.data.orderInfoVo.orderProNum}件商品"
                //合计金额
                binding.total.text = "合计：￥${it.data.orderInfoVo.proTotalFee}"
                //展示预下单详情
                getPreAdapter.data.clear()
                getPreAdapter.data.addAll(it.data.orderInfoVo.orderDetailList)
                getPreAdapter.notifyDataSetChanged()
            }else{
                ToastUtils.showLong(it.message)
            }
        }
        viewModel.createSuccess.observe(this){
            if(it.code==200){
                //amount 是为了在界面显示金额   orderNo是为了调用接口完成支付
                ARouter.getInstance().build("/app/PayActivity").withString("amount",binding.total.text.toString())
                    .withString("orderNo",it.data.orderNo).navigation()
            }else{
                ToastUtils.showLong(it.message)
            }
        }
    }

    override fun getViewModel(): Class<GetPreViewModel> = GetPreViewModel::class.java

    override fun getLayout(): Int = R.layout.activity_get_pre

}